package com.cn.kehong.service;


import com.cn.kehong.api.IProductDianciFenjiRecordService;
import com.cn.kehong.dao.ProductDianciFenjiRecordMapper;
import com.cn.kehong.domain.ProductDianciFenjiRecord;
import com.cn.kehong.domain.ProductDianciFenjiRecordExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductDianciFenjiRecordServiceImpl implements IProductDianciFenjiRecordService {

    @Autowired
    private ProductDianciFenjiRecordMapper productDianciFenjiRecordMapper;

    @Override
    public List<ProductDianciFenjiRecord> getProductDianciFenjiRecordList(QueryForm queryForm) {
        int count = productDianciFenjiRecordMapper.selectProductDianciFenjiRecordsCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductDianciFenjiRecord> productDianciFenjiRecords = productDianciFenjiRecordMapper.selectProductDianciFenjiRecords(queryForm);
        productDianciFenjiRecords.get(0).setCount(count);
        return productDianciFenjiRecords;
    }

    @Override
    public String addProductDianciFenjiRecord(ProductDianciFenjiRecord productDianciFenjiRecord) {
        String id = CommonUtil.generateRandomNum("dianrecord-");
        Map<String,String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        productDianciFenjiRecord.setBanCi(dutyMap.get("whiteDay"));
        productDianciFenjiRecord.setDutyDay(dutyMap.get("dutyDay"));
        productDianciFenjiRecord.setId(id);
        int count = productDianciFenjiRecordMapper.insertSelective(productDianciFenjiRecord);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateProductDianciFenjiRecord(ProductDianciFenjiRecord productDianciFenjiRecord) {
        String id = productDianciFenjiRecord.getId();
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        ProductDianciFenjiRecordExample example = new ProductDianciFenjiRecordExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciFenjiRecordMapper.updateByExampleSelective(productDianciFenjiRecord,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteProductDianciFenjiRecord(String id) {
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        ProductDianciFenjiRecord update = new ProductDianciFenjiRecord();
        update.setStatus(Constants.DEL_STATUS);
        ProductDianciFenjiRecordExample example = new ProductDianciFenjiRecordExample();
        example.createCriteria().andIdEqualTo(id);
        int count = productDianciFenjiRecordMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }
}
